﻿using System;
using System.Data;
using System.Xml;
using System.Configuration;
using System.Collections;
using System.Web;
using Winsteps.Validator;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using Model;
using Ysb.Components;
public partial class Admin_UserEditAll : System.Web.UI.Page
{
    private static XmlDocument Document = new XmlDocument();

    User user = new User();
    UserService users = new UserService();
    Province pv = new Province();
    ProvinceService pvs = new ProvinceService();
    City city = new City();
    CityService citys = new CityService();

    private string mm;
    protected void Page_Load(object sender, EventArgs e)
    {
        if (AdminCheck.Check(this, 36))
        {
            if (!Page.IsPostBack)
            {
                #region 页面初始化必需的内容
                Header.Title = ApplicationInfo.ApplicationName + "注册";
                HeaderTextInfo HeaderInfo = new HeaderTextInfo();
                HeaderInfo.DefaultStyle = Common.ConfigInfo.StyleSheet;
                HeaderInfo.GetDefaultHeader(this, null);
                #endregion

                DataLoad();

                EditLoad();
                BinList();
                anolb.Text = Request.QueryString["ano"].ToString();
                namelb.Text = Request.QueryString["name"].ToString();


            }
        }
        else
        {
            Response.Redirect(AdminCheck.RedirectPage);
        }


    }

    private void DataLoad()
    {

        gsdlist.DataSource = new CountryService().GetCountry();
        gsdlist.DataTextField = "name";
        gsdlist.DataValueField = "id";
        gsdlist.DataBind();
        gsdlist.Items.Insert(0, "请选择国家");

        payselect.DataSource = new PublicSqlService().GetData("Name,ID", "Tb_SalesUer", "");
        payselect.DataTextField = "Name";
        payselect.DataValueField = "ID";
        payselect.DataBind();







    }
    private void BinList()
    {


        RatesList.DataSource = new WayBillService().GetCalType(Request.QueryString["id"].ToString());
        RatesList.DataBind();
    }

    private void EditLoad()
    {


        user = users.GetUser(Convert.ToInt32(Request.QueryString["id"]));
        mm = user.PassWord;
        lnametxt.Text  = user.LoginName;
        realname.Value = user.TrueName;
        emailtxt.Value = user.Email;

        if (user.Sex == 1)
        {
            man.Checked = true;
            woman.Checked = false;

        }
        else
        {
            woman.Checked = true;

            man.Checked = false;
        }
        // gsdlist.SelectedIndex = gsdlist.Items.IndexOf(gsdlist.Items.FindByText(user.Country.Trim()));
        gsdlist.Text = user.CountryID;
        //sexrd.SelectedIndex=sexrd.Items.IndexOf(sexrd.Items.FindByValue(user.Sex.ToString()));
        phonetxt.Value = user.Mobile;
        teltxt.Value = user.Tel;
        comptxt.Value = user.Company;
        addressxtxt.Value = user.Address;
        ybtxt.Value = user.ZipCode;
        chuanzhentxt.Value = user.FaxNo;
        city_txt.Value = user.City;

        qqtxt.Value = user.QQ;
        idcard.Value = user.IDCardNO;
        slist.DataSource = pvs.GetByid(new Tools().StringToIntDif(gsdlist.SelectedItem.Value.Trim()));
        slist.DataTextField = "name";
        slist.DataValueField = "code";
        slist.DataBind();
        slist.Items.Insert(0, "请选择省份");
        //slist.SelectedIndex = slist.Items.IndexOf(slist.Items.FindByText(user.State));
        slist.Text = user.StateID;
        DataTable dt = new PublicSqlService().GetData("*", "Tb_UserProperty", "UserID=" + Request.QueryString["id"].Trim());

        SalesRatetxt.Value = dt.Rows[0]["SalesRate"].ToString();
        payselect.SelectedIndex = payselect.Items.IndexOf(payselect.Items.FindByValue(dt.Rows[0]["SalesID"].ToString()));
        ShoppingServiceRatetxt.Value = dt.Rows[0]["ShoppingServiceRate"].ToString();
        PackagingRatetxt.Value = dt.Rows[0]["PackagingRate"].ToString();

        if (Session["UserName"].ToString() == "Tom Liu" || Session["UserName"].ToString() == "小容" || Session["UserName"].ToString() == "steven" || Session["UserName"].ToString() == "Chris Wu"
   )
                 {
        ArrearsLimittxt.Value = dt.Rows[0]["ArrearsLimit"].ToString();
                 }
                 else
                 {
      ArrearsLimittxt.Value = dt.Rows[0]["ArrearsLimit"].ToString();
       ArrearsLimittxt.Attributes.Add("readonly", "readonly");


                 }



    }
    protected void okbtn_Click(object sender, ImageClickEventArgs e)
    {

        if (realname.Value.Trim() == "")
        {

            Tools.OpenWindow(this, "请录入用户姓名");
            return;
        }
 

        if (addressxtxt.Value.Trim() == "")
        {

            Tools.OpenWindow(this, "请录入地址");
            return;
        }

        if (teltxt.Value.Trim() == "")
        {

            Tools.OpenWindow(this, "请输入电话号码");
            return;
        }
 



        user.Id = Convert.ToInt32(Request.QueryString["id"]);
        user.LoginName = lnametxt.Text;
        user.TrueName = realname.Value;
        user.Email = emailtxt.Value;

        if (man.Checked)
        {
            user.Sex = 1;

        }
        if (woman.Checked)
        {
            user.Sex = 0;
        }

        user.Mobile = phonetxt.Value.Trim();
        user.Tel = teltxt.Value.Trim();

        user.IDCardNO = idcard.Value.Trim();
        user.QQ = qqtxt.Value;
        user.Company = comptxt.Value.Trim();
        user.Country = gsdlist.SelectedItem.Text.Trim();
        user.CountryID = gsdlist.SelectedValue;

        user.State = slist.SelectedItem.Text.Trim();
        user.StateID = slist.SelectedValue;
        user.City = city_txt.Value;
        user.Address = addressxtxt.Value.Trim();
        user.ZipCode = ybtxt.Value.Trim();
        user.FaxNo = chuanzhentxt.Value.Trim();
        user.UserType = int.Parse(payselect.SelectedValue);
        user.PassWord = StrHelper.EncryptPassword(passtxt.Value.Trim(), StrHelper.PasswordType.MD5);


        if (users.AdminUpdata(user) == 1)
         
        {


            Hashtable hs = new Hashtable();

            hs.Add("SalesID", payselect.SelectedValue);

            hs.Add("SalesRate", new Tools().StringToDecimalDif(SalesRatetxt.Value));


            hs.Add("ShoppingServiceRate", new Tools().StringToIntDif(ShoppingServiceRatetxt.Value));


            hs.Add("PackagingRate", new Tools().StringToDecimalDif(PackagingRatetxt.Value));



            hs.Add("ArrearsLimit", new Tools().StringToDecimalDif(ArrearsLimittxt.Value));


            hs.Add("PostDate", DateTime.Now);



            if (new PublicSqlService().Update(hs, "Tb_UserProperty", "UserID=" + Request.QueryString["id"].ToString()))
            {
                ToUpRate();
                Tools.OpenWindow(this, "信息保存成功");
            }
            else
            {
                Tools.OpenWindow(this, "用户基本资料编辑成功！但费用信息编辑失败");
            }
            //有 修改


        }
        else
        {
            Tools.OpenWindow(this, "用户信息编辑失败");
        }


    }
    protected void gjlist_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (gsdlist.SelectedItem.Value.Trim() != "请选择国家")
        {
            slist.DataSource = pvs.GetByid(Convert.ToInt32(gsdlist.SelectedValue));
            slist.DataTextField = "name";
            slist.DataValueField = "code";
            slist.DataBind();
            slist.Items.Insert(0, "请选择省份");
        }
    }

    private void ToUpRate()
    {
        Hashtable hs = new Hashtable();
        Tools t = new Tools();
        PublicSqlService ps = new PublicSqlService();
        for (int i = 0; i < RatesList.Items.Count; i++)
        {
            hs.Clear();
            Label lb = (Label)RatesList.Items[i].FindControl("statelb");
            Label namelb = (Label)RatesList.Items[i].FindControl("namelb");
            HtmlInputText fist = (HtmlInputText)RatesList.Items[i].FindControl("GeneralRatesFirsttxt");
            HtmlInputText last = (HtmlInputText)RatesList.Items[i].FindControl("GeneralRatesLasttxt");
            hs.Add("SB", t.StringToDecimalDif(fist.Value));
            hs.Add("CB", t.StringToDecimalDif(last.Value));
            if (lb.ToolTip.Trim() == "")
            {
                hs.Add("CalTypeID", namelb.ToolTip.Trim());
                hs.Add("Userid", Request.QueryString["id"].ToString());
                ps.Insert(hs, "Tbl_CalTypeUser");


                //录入
            }
            else
            {
                ps.Update(hs, "Tbl_CalTypeUser", "ID=" + lb.ToolTip.Trim());

                //编辑
            }

        }
    }


}
